<?php

namespace App\Listeners;

use App\Events\NoticeUserEvent;
use App\Models\V1\NoticeUser;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class NoticeUserEventListener
{
    /**
     * @var NoticeUser
     */
    private $noticeUser;

    /**
     * Create the event listener.
     *
     * @param NoticeUser $noticeUser
     */
    public function __construct(NoticeUser $noticeUser)
    {
        $this->noticeUser = $noticeUser;
    }

    /**
     * Handle the event.
     *
     * @param  NoticeUserEvent $event
     * @return void
     */
    public function handle(NoticeUserEvent $event)
    {
        $sumupUserId   = $event->sumupUserId;//写这个读书改进计划的人的user_id
        $commentUserId = $event->commentUserId;//评论用户的user_id
        $sumUpId       = $event->sumUpId;//读书改进计划的ID
        $readPartyId   = $event->readPartyId;//读书会的ID
        $userName      = $event->userName;//要通知的用户的名字
        $avg           = $event->avg;//要通知的用户的头像
        $content       = emoji_text_encode($event->content);//要通知的内容
        if (!$commentUserId && $sumupUserId) {//只通知该读书改进计划的人
            $noticeInfo = $this->noticeUser->create([
                'notice_user_id' => $sumupUserId,
                'sum_up_id'      => $sumUpId,
                'read_party_id'  => $readPartyId,
                'user_name'      => $userName,
                'avg'            => $avg,
                'content'        => $content
            ]);
            return $noticeInfo;
        } else if ($commentUserId && $sumupUserId) {//通知写评论的这个人
            if ($sumupUserId != $commentUserId) {
                $this->noticeUser->create([
                    'notice_user_id' => $sumupUserId,
                    'sum_up_id'      => $sumUpId,
                    'read_party_id'  => $readPartyId,
                    'user_name'      => $userName,
                    'avg'            => $avg,
                    'content'        => $content
                ]);
                $noticeInfo = $this->noticeUser->create([
                    'notice_user_id' => $commentUserId,
                    'sum_up_id'      => $sumUpId,
                    'read_party_id'  => $readPartyId,
                    'user_name'      => $userName,
                    'avg'            => $avg,
                    'content'        => $content
                ]);
                return $noticeInfo;
            } else {
                $noticeInfo = $this->noticeUser->create([
                    'notice_user_id' => $sumupUserId,
                    'sum_up_id'      => $sumUpId,
                    'read_party_id'  => $readPartyId,
                    'user_name'      => $userName,
                    'avg'            => $avg,
                    'content'        => $content
                ]);
//                $noticeInfo = $this->noticeUser->create([
//                    'notice_user_id' => $commentUserId,
//                    'sum_up_id'      => $sumUpId,
//                    'read_party_id'  => $readPartyId,
//                    'user_name'      => $userName,
//                    'avg'            => $avg,
//                    'content'        => $content
//                ]);
                return $noticeInfo;
            }
        } else if (!$sumupUserId && $commentUserId) {
            $noticeInfo = $this->noticeUser->create([
                'notice_user_id' => $commentUserId,
                'sum_up_id'      => $sumUpId,
                'read_party_id'  => $readPartyId,
                'user_name'      => $userName,
                'avg'            => $avg,
                'content'        => $content
            ]);
            return $noticeInfo;
        }
    }
}
